//
//  CommonBtnView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/24.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "CommonBtnView.h"

@implementation CommonBtnView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        //titleLabel.text = @"地址*";
        titleLabel.font = MidBigFont;
        titleLabel.textColor = CSColorFromRGB(0x04030B);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UILabel *choseLabel = [[UILabel alloc] init];
        //titleLabel.text = @"地址*";
        choseLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        choseLabel.textAlignment = NSTextAlignmentRight;
        choseLabel.font = MidBigFont;
        choseLabel.textColor = PlaceHoldColor;
        choseLabel.text = @"请选择";
        [self addSubview:choseLabel];
        self.choseLabel = choseLabel;
        
        UIImageView *rightImageView = [[UIImageView alloc] init];
        rightImageView.image = [UIImage imageNamed:@"xiayibu"];
        [self addSubview:rightImageView];
        self.rightImageView = rightImageView;
        
        UIButton *choseBtn = [[UIButton alloc] init];
        [choseBtn setTitleColor:PlaceHoldColor];
        [self addSubview:choseBtn];
        self.choseBtn = choseBtn;
        
        UIView *lineView= [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [self addSubview:lineView];
        self.lineView = lineView;;
        
        [titleLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.choseBtn);
        
        make.left.mas_equalTo(self).offset(10);
        
//        make.width.mas_equalTo(100);
    }];
    
    
    [self.choseBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-5);
        
        make.top.bottom.mas_equalTo(self);
        
        make.height.mas_equalTo(60);
        
        make.width.mas_equalTo(100);
    }];
    
    [self.choseLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.choseBtn);
        
        make.right.mas_equalTo(self).offset(-25);
        
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(10);
        
    }];
    
    //设置优先级titleLabel完全显示titleLabel过长。。。
    [self.titleLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultHigh) forAxis:(UILayoutConstraintAxisHorizontal)];
    [self.choseLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
    
    [self.rightImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self).offset(-10);
        
        make.centerY.mas_equalTo(self);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.right.mas_equalTo(self);
        
        make.height.mas_equalTo(1);
    }];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSDictionary * dict=[NSDictionary dictionaryWithObject: self.titleLabel.font forKey:NSFontAttributeName];
    CGRect rect=[self.titleLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(rect.size.width + 10);
        
    }];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    [self strWithRed];
    
    
    CGSize titleSize = [self.titleLabel.text sizeWithFont:MidFont maxSize:CGSizeMake(MAXFLOAT, 30)];
    
    if (titleSize.width >= 90) {
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.mas_equalTo(self.choseBtn);
            
            make.left.mas_equalTo(self).offset(10);
            
            make.width.mas_equalTo(titleSize.width + 40);
        }];
    }
}


-(void)strWithRed{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];
    
    [str addAttribute:NSForegroundColorAttributeName value:CSRedColor range:[self.titleLabel.text rangeOfString:@"*"]];
    
    self.titleLabel.attributedText = str;
}

-(void)dealloc{
    
    [self.titleLabel removeObserver:self forKeyPath:@"text"];
    self.titleLabel = nil;
}
@end
